package com.jspgou.core.entity;

import com.jspgou.common.web.session.SessionProvider;
import com.jspgou.core.entity.base.BaseWebsite;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

public class Website extends BaseWebsite{
    private static final long serialVersionUID = 1L;
    
    public static final String RES_BASE = "r";
//  public static final String USER_BASE = "user_base";
    public static final String USER_BASE = "t";
    public static final String FRONT_RES = "/res/front";
    public static final String UPLOAD_PATH = "u";
    public static final String MOBILE_UPLOAD_PATH= "mobile_u";
//  public static final String TEMPLATE_PATH = "template";
    public static final String TEMPLATE_PATH = "tpl";
    public static final String TEMPLATE_MOBILE_PATH = "mobile";
    public static final String DEFAULT = "default";
    public static final String SPT = "/";
    public static final String TPL_SUFFIX = ".html";
    public static final String TPL_PREFIX_SYS = "sys_";
    public static final String TPL_PREFIX_TAG = "tag_";
    public static final String STORE_TEMPLATE_PATH = "store";
    public static final String PC= "pc";
    
    
	public String getLoginUrl() {
		Global global = getGlobal();
		if (global != null) {
			return global.getLoginUrl();
		} else {
			return null;
		}
	}

	public String getProcessUrl() {
		Global global = getGlobal();
		if (global != null) {
			return global.getProcessUrl();
		} else {
			return null;
		}
	}

    public String getTemplate(HttpServletRequest request,String dir, String name, String s2, String s3){
    	String equipment=(String) request.getAttribute("ua");
    	StringBuilder stringbuilder = getTemplateRelBuff().append("/").append(dir).append("/");
    	if(StringUtils.isNotBlank(equipment)&&equipment.equals("mobile")){
    		stringbuilder = getTemplateMobileRelBuff().append("/").append(dir).append("/");
    	}
        if(!StringUtils.isBlank(s3)){
            stringbuilder.append(s3);
        }
        stringbuilder.append(name);
        if(!StringUtils.isBlank(s2)){
            stringbuilder.append("_").append(s2);
        }
        return stringbuilder.append(TPL_SUFFIX).toString();
    }
    
    public String getStoreTemplate(String tmp,String dir, String name, String s2, String s3, HttpServletRequest request){
      String equipment=(String) request.getAttribute("ua");
      StringBuilder stringbuilder = getStoreTemplateRelBuff(STORE_TEMPLATE_PATH,tmp).append("/").append(dir).append("/");
      if(StringUtils.isNotBlank(equipment)&&equipment.equals("mobile")){
    	  stringbuilder = getStoreTemplateMobileRelBuff(STORE_TEMPLATE_PATH,tmp).append("/").append(dir).append("/");
      }
      if(!StringUtils.isBlank(s3)){
          stringbuilder.append(s3);
      }
      stringbuilder.append(name);
      if(!StringUtils.isBlank(s2)){
          stringbuilder.append("_").append(s2);
      }
      return stringbuilder.append(TPL_SUFFIX).toString();
  }

    //获取原来的develop
    @SuppressWarnings("unchecked")
	public String getSolution(String s){
        Map map = getSolutions();
        if(map == null){
            throw new IllegalStateException("cannot getSolutions(), may be the object not a persistent object.");
        }
        String s1 = (String)map.get(s);
        if(StringUtils.isBlank(s1)){
            return DEFAULT;
        }else{
            return s1;
        }
    }

    public String getUrl(){
        return getUrlBuff(false).append("/").toString();
    }
    
    //0000000000000000
    public String getUploadRel(String s){
        StringBuilder stringbuilder = getResBaseUBuff();
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    public String getUploadRelCut(String s){
    	StringBuilder stringbuilder = getResBaseUBuffCut().append("/");
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    public String getUploadRel1(String s){
    	StringBuilder stringbuilder = getResBaseRelBuff().append("/");
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    //后台图片集取图片路径
    public String getUploadUrl(String s){
        StringBuilder stringbuilder = getAdminRes();
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    public String getTTemplateRel(){
    	StringBuilder stringbuilder = null;
    	stringbuilder = getTemplateBuff();
         return stringbuilder.toString();
    }
    
    public String getTemplateRel(String s, HttpServletRequest request){
    	StringBuilder stringbuilder = null;
        stringbuilder = getTemplateRelBuff();
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    public String getStoreTemplateRel(String storePath,String s, HttpServletRequest request){
    	String equipment=(String)request.getAttribute("ua");
        StringBuilder stringbuilder = getStoreTemplateRelBuff("store",storePath);
        if(StringUtils.isNotBlank(equipment)&&equipment.equals("mobile")){
        	stringbuilder=getStoreTemplateMobileRelBuff("store",storePath);
        }
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    //0000000000000000
    public String getResBaseRel(String s){
        StringBuilder stringbuilder = getResBaseRelBuff();
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }
    
    //00000000000000000000
    public String getResBaseUrl(String s){
        StringBuilder stringbuilder = getResBaseUrlBuff();
        if(!StringUtils.isBlank(s)){
            if(!s.startsWith("/")){
                stringbuilder.append("/");
            }
            stringbuilder.append(s);
        }
        return stringbuilder.toString();
    }

    public String getFrontResUrl(){
        return getUrlBuff(false).append(FRONT_RES).toString();
    }

    private StringBuilder getUserBaseRelBuff(){
        return (new StringBuilder(SPT)).append("WEB-INF").append(SPT).append(USER_BASE);
    }

    private StringBuilder getResBaseRelBuff(){
        return (new StringBuilder(SPT)).append(RES_BASE).append(SPT).append(getResPath());
    }
    
    private StringBuilder getResBaseUBuff(){
    	return (new StringBuilder(SPT).append(UPLOAD_PATH));
    }
    
    private StringBuilder getResBaseUBuffCut(){
    	return (new StringBuilder(SPT));
    }
    
    //00000000000000000000
    public String getResBaseUrl(HttpServletRequest request){
    	String equipment=(String) request.getAttribute("ua");
    	if(equipment==null){
    		return getResBuff().toString();
    	}else if(StringUtils.isNotBlank(equipment)&&equipment.equals("mobile")){
    		return getResBaseUrlMobileBuff().toString();
    	}else if(StringUtils.isNotBlank(equipment)&&equipment.equals("pc")){ 
    		return getResBaseUrlBuff().toString();
    	}
		return null;
    }
    
    //dzd后台取图片路径
    private StringBuilder getAdminRes(){
    	return getUrlBuff(false);
    }
    
    //pc端移动端共同资源文件夹
    private StringBuilder getResBuff(){
        return getUrlBuff(false).append(SPT).append(RES_BASE).append(SPT).append(getResPath());
    }
    
    //pc端资源文件
    private StringBuilder getResBaseUrlBuff(){
        return getUrlBuff(false).append(SPT).append(RES_BASE).append(SPT).append(getResPath()).append(SPT).append(PC);
    }
    
    //移动端资源文件
    private StringBuilder getResBaseUrlMobileBuff(){
        return getUrlBuff(false).append(SPT).append(RES_BASE).append(SPT).append(getResPath()).append(SPT).append(TEMPLATE_MOBILE_PATH);
    }
    
    public String getStoreResBaseUrl(String storePath, HttpServletRequest request){
        return getStoreResBaseUrlBuff(storePath,request).toString();
    }
    
    private StringBuilder getStoreResBaseUrlBuff(String storePath,HttpServletRequest request){
    	String equipment=(String) request.getAttribute("ua");
    	if(StringUtils.isNotBlank(equipment)&&equipment.equals("mobile")){
    		return getUrlBuff(false).append(SPT).append(RES_BASE).append(SPT).append("store").append(SPT).append(MOBILE_UPLOAD_PATH).append(SPT).append(storePath);
    	}
        return getUrlBuff(false).append(SPT).append(RES_BASE).append(SPT).append("store").append(SPT).append(UPLOAD_PATH).append(SPT).append(storePath);
    }
    
    public StringBuilder getTemplateBuff(){
        StringBuilder stringbuilder = getUserBaseRelBuff().append(SPT).append(getResPath()).append(SPT);
        return stringbuilder;
    }
    
    //pc端路径
    public StringBuilder getTemplateRelBuff(){
        StringBuilder stringbuilder = getUserBaseRelBuff().append(SPT).append(getResPath()).append(SPT).append(TEMPLATE_PATH);
        return stringbuilder;
    }
    
    //移动端路径
    public StringBuilder getTemplateMobileRelBuff(){
    	StringBuilder stringbuilder=getUserBaseRelBuff().append(SPT).append(getResPath()).append(SPT).append(TEMPLATE_MOBILE_PATH);
    	return stringbuilder;
    }
    
    //pc端店铺入口
    public StringBuilder getStoreTemplateRelBuff(String storePath,String tmp){
        StringBuilder stringbuilder = getUserBaseRelBuff().append(SPT).append(storePath).append(SPT).append(UPLOAD_PATH).append(SPT).append(tmp);
        return stringbuilder;
    }
    
    //移动端店铺入口
    public StringBuilder getStoreTemplateMobileRelBuff(String storePath,String tmp){
        StringBuilder stringbuilder = getUserBaseRelBuff().append(SPT).append(storePath).append(SPT).append(MOBILE_UPLOAD_PATH).append(SPT).append(tmp);
        return stringbuilder;
    }

    public String getTemplateRel(HttpServletRequest request){
        return getTemplateRel(null, request);
    }

    public StringBuilder getUrlBuff(boolean flag){
        StringBuilder stringbuilder = new StringBuilder();
        if(flag || !getRelativePath().booleanValue()){
            stringbuilder = (new StringBuilder("http://")).append(getDomain());
            Integer integer = getGlobal().getPort();
            if(integer != null && integer.intValue() != 80){
                stringbuilder.append(":").append(integer);
            }
        }
        if(getContextPath()!=null){
        stringbuilder.append(getContextPath());
        }
        return stringbuilder;
    }

    public String getResBaseRel(){
        return getResBaseRelBuff().toString();
    }

    public String getUploadRel(){
        return getUploadRel(null);
    }

    public String getUploadUrl(){
        return getUploadUrl(null);
    }
    
    public String getTemplate(String dir, String name, HttpServletRequest request){
        return getTemplate(request,dir, name, null, null);
    }
    
    public String getStoreTemplate(String tmp,String dir, String name, HttpServletRequest request){
        return getStoreTemplate(tmp,dir, name, null, null, request);
    }

    public String getTplSys(String dir, String name, HttpServletRequest request){
    	//加sys_
//        return getTemplate(s, s1, null, TPL_PREFIX_SYS);
        return getTemplate(request,dir, name, null, null);
    }

    public String getTplTag(String dir, String name, String s2){
    	//加tag_
//        return getTemplate(s, s1, s2, TPL_PREFIX_TAG);
          return getTemplate(null,dir, name, s2, null);
    }

    public String getContextPath(){
        String s = getGlobal().getContextPath();
        return StringUtils.isBlank(s) ? "" : s;
    }

    public String[] getDomainAliasArray(){
        String s = getDomainAlias();
        if(!StringUtils.isBlank(s)){
            return s.split(",");
        }else{
            return null;
        }
    }
	
	/* [CONSTRUCTOR MARKER BEGIN] */
    public Website(){
    	super();
    }

	/**
	 * Constructor for primary key
	 */
    public Website(Long id){
        super(id);
    }

	/**
	 * Constructor for required fields
	 */
    public Website(Long id, Global global, String domain,String resPath, String name,
    		String currentSystem, String suffix, Integer lft, Integer rgt, Date createTime, 
            Boolean close, Boolean relativePath, String frontEncoding, String frontContentType, 
            String localeFront, String localeAdmin, Integer controlNameMinlen, String company, 
            String copyright, String recordCode, String email, String phone, String mobile){
        super(id, global, domain, resPath, name, currentSystem, suffix, lft, 
        		rgt, createTime, close, relativePath, frontEncoding, frontContentType, localeFront,
        		localeAdmin, controlNameMinlen, company, copyright,recordCode, email, phone, mobile);
    }
	/* [CONSTRUCTOR MARKER END] */
}
